import 'package:aio/aio.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:app/generated/app_strings.dart';
import 'package:app/src/config/app_data_config.dart';
import 'package:app/src/http/bean/wroks/search_bean.dart';
import 'package:app/src/ui/app_ui.dart';
import 'package:app/src/http/bean/ywx_limit.dart';

import '../../../../../http/bean/worksdo/equipment_management_bean.dart';
import '../../../../../manager/ywx_manager.dart';
import 'ui/review_list.dart';

///我的工单；工单领用（领用记录）
class TreasuryListRoute extends StatefulWidget {
  YwxItem item;
  EquipmentInfo equipmentInfo;

  TreasuryListRoute(this.item, this.equipmentInfo);

  @override
  _State createState() => _State();
}

class _State extends State<TreasuryListRoute> {
  _Presenter presenter = _Presenter();

  @override
  void initState() {
    super.initState();
    // print(
    //     "搜索条件菜单（开始）初始化：==============================================${presenter.searchInfo}");
    if (presenter.searchInfo == null) {
      presenter.searchInfo = SearchInfo(widget.item);
      presenter.searchInfo?.factory = AppDataConfig.getFactory(); //获取当前默认工厂
      presenter?.searchInfo?.equipmentInfo = widget.equipmentInfo;
    }
    initSearchInfo();
    // widget?.item?.ReviewType = ReviewType
    //     .Contract; //=============================fixme 这里用合同审核作为测试数据；后面记得要改过哦。
  }

  @override
  void dispose() {
    // TODO: implement dispose
    super.dispose();
    // widget?.item?.ReviewType = ReviewType
    //     .MaterialRequisition; //=============================fixme 恢复一下
  }

  ///是否切换了菜单
  bool isSwitchMenu = false;

  initSearchInfo() {
    isSwitchMenu = true;
    // print(
    //     "搜索条件菜单切换：==============================================${presenter.searchInfo}");
    if (presenter.searchInfo != null) {
      presenter.searchInfo!..isShowStatus = false; //审核状态
      presenter.searchInfo!.isShowAddTime = false; //日期
      presenter.searchInfo!.isShowReceivingStatus = true; //领用单状态
    }
  }

  @override
  Widget build(BuildContext context) {
    px.init(context);
    // initSearchInfo(context);
    var scaffold = AppUi.getScaffoldWithReView(
        context, _body(context), widget.item, presenter.searchInfo!, () {
      //print("===============================================================搜索刷新");
      presenter.setStateBody?.call(() {});
    },
        title: AppStrings.of(context).Myworkorder +
            ":" +
            AppStrings.of(context).Receiptrecord); //标题：我的工单：领用记录
    return WillPopScope(
      child: scaffold,
      onWillPop: () async {
        return true; //true 不屏蔽返回键
      },
    );
  }

  Widget _body(BuildContext context) {
    //print("===============================================================页面刷新:$isShowPopuMenuWithSearch");
    return StatefulBuilder(
        builder: (BuildContext context, StateSetter setState) {
      presenter.setStateBody = setState;
      List<Widget> children = [];
      //搜索条件
      var searchLayout =
          AppUi.getSearchLayout(context, presenter.searchInfo!, () {
        presenter.setStateBody?.call(() {});
      }, ywxItem: widget.item, ywxItemCallback: () {});
      children.add(searchLayout);
      //审核列表
      var reviewList =
          ReviewList2(widget.item, presenter.searchInfo!, true, isSwitchMenu);
      if (isSwitchMenu) {
        isSwitchMenu = false;
      }
      children.add(Expanded(child: reviewList));
      return Column(
        children: children,
      );
    });
  }
}

class _Presenter {
  ///搜索条件
  SearchInfo? searchInfo = null;

  ///body视图状态
  StateSetter? setStateBody;
}
